<?php
/**
 * 图片处理控制器
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/1/16
 * Time: 19:07
 */
Class pictureAction extends CommonAction{

     public function index(){
           $loginId = _decrypt(cookie('loginId'));
           $dataCount = M('Image')->where(array("uid"=>$loginId))->count();
           $data = D("Image")->getDataByMore(array("uid"=>$loginId),'','sort');
           foreach($data as $k => $v)
                $data[$k]['groupname'] = M("ImageGroup")->where(array("id"=>$v['cid']))->getField("cname");
           $this->dataCount = $dataCount;
           $this->data = $data;
           $this->display("picture-list");
     }

     //添加图片
     public function addImage(){
         $title = $this->_post("title");
         $mintitle = $this->_post('min-title');
         $data['key_word'] = $this->_post("key_word");
         $data['cid'] = $this->_post("p_group");
         $data['sort'] = 1;
         $data['uid'] = _decrypt(cookie('loginId'));
         if($this->_post('id')){
              $data['title'] = $title;
              $data['mintitle'] = $mintitle;

         }else {
             if (!strpos($title, ',')) {
                 $title .= ',';
             }
             if (!strpos($mintitle, ',')) {
                 $mintitle .= ',';
             }
             $titleArr = explode(',', $title);
             $mintitleArr = explode(',', $mintitle);
             $Flies = $this->unEmpty($_FILES['filepic']);
             $Flies = $this->FormatFiles($Flies);
             $title = D('Image')->toNumFormat($titleArr, count($Flies['name']));
             $mintitle = D("Image")->toNumFormat($mintitleArr, count($Flies['name']));
             $w_num = 0;
             foreach ($Flies as $k => $v) {
                 $fileUploadRoot = "Data/Upload/photos/";
                 $res = $this->_uploadfile($v, $fileUploadRoot);
                 if ($res) {
                     $data['content'] = $res['savepath'];
                     $data['title'] = $title[$k];
                     $data['mintitle'] = $mintitle[$k];
                     $data['time'] = time();
                     // 写入数据
                     $reslute = D('Image')->addImage($data);
                     if ($reslute) {
                         $w_num++;
                     }
                 }
             }
             if ($w_num == count($Flies)) {
                 $this->show("success", U("admin/picture/Index"), "添加成功");
             } else {
                 $this->show("error", U('admin/picture/picture_add_view'), "添加失败");
             }
         }
     }

     public function picture_add_view(){
           $cata = D('ImageGroup')->getCategory();
           if($_GET['id']){
                  $id = $_GET['id'];
                  $data = D('Image')->getDataByOne(array("id"=>$id));
                  $cate = $this->createOptionHtml($cata,$data['cid']);
                  $this->data =$data;
           }else {
               $cate = $this->createOptionHtml($cata);
           }
           $this->cate = $cate;
           $this->display("picture-add");
     }

     // 删除图片
     public function delImage(){
          $delId = rtrim($_GET['id'],"|");
          if(strpos($delId,'|')){
                $delId = explode("|",$delId);
          }
          print_r($delId);
          die;
          $delNum = 0;
          if(is_array($delId)){
              $num = count($delId);
              foreach($delId as $id){
                  $res = D('Image')->delImage($id);
                  if($res) { $delNum++; }
              }
          }else{
              $num = 1;
              $res = D('Image')->delImage($delId);
              if($res) { $delNum++; }
          }
          if($delNum == $num){
               echo 1;
          }
     }

     // 相册分组列表
     public function ImageGrouplist(){
         $data = D('ImageGroup')->getDataByMore();
         $data = D("ImageGroup")->getImageData($data);
         $this->data = $data;
//         var_dump($data);
         $this->display("picture-type");
     }

     /**
      * 添加相册分组 / 修改
      */
     public function addImageGroup(){
          $data['cname'] = $this->_post("grouname");
          $data['min-title'] = $this->_post("group-title");
          $data['style'] = $this->_post("group-style");
          $data['pid'] = !empty($this->_post("group_pid"))?$this->_post("group_pid"):0;
          $data['key_word'] = $this->_post("group_keyword");
          $data['intro'] = $this->_post("group_intro");
          $data['uid'] = _decrypt(cookie('loginId'));
          $data['createtime'] = time();

          if($_FILES['file']['error']!=4){
                 $path = "Data/Upload/cover/";
                 $res = $this->_uploadImage($_FILES['file'],$path,200,200);
                 if(!empty($res)){
                      $data['cover'] = $res["savepath"];
                 }
          }
          if($this->_post('id')){
              $id = $this->_post('id');
              $res = D('ImageGroup')->updataImageGroup(array("id"=>$id),$data);
              $str = "修改";
          }else {
              $res = D('ImageGroup')->addImageGroup($data);
              $str = "添加";
          }
          if($res){
                $this->show("success",U('admin/picture/ImageGrouplist'),$str."成功");
          }else{
                $this->show("error",U('admin/picture/addImageView'),$str."失败");
          }
     }

     /**
      * 添加相册分组视图
      */
     public function addImageView(){
         if($this->_get('id')){
               $id = $this->_get('id');
               $cate = D('ImageGroup')->getCategory();
               $data = D('ImageGroup')->getDataByOne(array("id=$id"));
               $cate = $this->createOptionHtml($cate,$data['pid']);
               $this->data = $data;
         }else {
               $data = D('ImageGroup')->getCategory();
               $cate = $this->createOptionHtml($data);
         }
         $this->cate = $cate;
         $this->display("picture-type-add");
     }

     /**
      * 删除相册分组 通过id
      */
     public function deleteImage(){
          $id = $_GET['id'];
          $res = array(
              "code" =>200,
              "msg" =>"success",
          );
//          $res = D("ImageGroup")->delImageGroup($id);
          echo(json_encode($res));
     }

     /**
      *
      * 用于去除FILE 为空的数组
      */
     private function unEmpty($data){
         $newdata = array();
         foreach($data as $k => $v){
             foreach($v as $key => $value){
                   if($value === '' || $value == 4){
                       array_splice($v,$key,1);
                   }
             }
             if($k=='size'){
                   foreach($v as $ke => $value){
                           if($value==0){
                                array_splice($v,$ke,1);
                           }
                   }
             }
             $newdata[$k] = $v;
         }
         return $newdata;
     }

    /**
     * 重组数组
     * @param $Files
     */
    private function FormatFiles($Files){
         $newFiles = Array();
         $offset = 0;
         $index = 0;
         for($i=0;$i<count($Files['name']);$i++) {
             foreach ($Files as $key => $v) {
                 $newFiles[$offset][$key] = $v[$index];
             }
             $offset++;
             $index++;
         }
         return $newFiles;
    }
}